
# Data Generator


import matplotlib.pyplot as plt
import numpy as np
import csv
import sys
import math
import random
import seaborn as sns

from models import *

Gerro    = 0.0
Gidxctrl = 0
Ghistoryctrl  = []

Gcbase = 0.4
Gc = 0.4


#----------------------------------------------------
def mul (num,v):
   return [valor * num for valor in v]

#----------------------------------------------------
def add (v1,v2):
   n = len(v1)
   v = []
   for k in range(n):
      v.append (v1[k] + v2[k])
   return v

#----------------------------------------------------
def RK (f, t, dt, s) :

#   t = 0
   one6 = 1./6.
   one3 = 1./3.
   h = dt
   t0 = t
   half    = h * 0.5
   t_h     = t0 + h
   t_half  = t0 + half

   v1 = mul (h , f (t0, s))
   vt = add (v1, mul (0.5, v1))

   v2 = mul (h , f (t_half, vt))
   vt = add (v2, mul (0.5, v1))

   v3 = mul (h , f (t_half, vt))
   vt = add (s, v3)

   v4 = mul (h , f (t_h   , vt))

#   vt = add (s, add( \
#         mul (one6, v1), add( \
#         mul (one3, v2), add( \
#         mul (one3, v3), \
#         mul (one6, v4)))))

   vt = add (s, \
          add( mul (one6, v1), \
            add( mul (one3, v2), \
              add (mul (one3, v3), mul (one6, v4)))))

   return vt






#==============================================================

dt = 1.0 / 256.0

Ghistoryctrl = [Gc]

s  = [10., 10.]
Mx = []
My = []


for t in range(10000):
    o = RK (LotkaVolterraModif, t, dt, s)
    Mx.append(o[0])
    My.append(o[1] * random.gauss(1.0, 0.001) + 0.125 * math.sin(math.pi * t / 2000.))
    s = o

s    = [10., 10.]
Tx = []
Ty = []
for t in range(10000):
    o = RK (LotkaVolterra, t, dt, s)
    Tx.append(o[0])
    Ty.append(o[1])
    s = o

with open("Reference.csv", 'wt') as myfile:
    wr = csv.writer(myfile)
    wr.writerow(Mx)
    wr.writerow(My)



stitle = 'Lotka-Volterra ' # + r'$\beta = ' + str(GBeta) + '$'

#'Lotka-Volterra ' + r'$\beta = 0.1$'
plt.title (stitle, fontsize=18)
plt.plot(Tx, Ty, lw=1.5, color='green' , linestyle = 'dotted', label='Base model')
plt.plot(Mx, My, lw=1.5, color='blue'  , linestyle = 'dashed', label='Reference')

plt.grid ()


plt.subplots_adjust(
top=0.88,
bottom=0.13,
left=0.125,
right=0.9,
hspace=0.2,
wspace=0.2
)


plt.legend()
plt.xlabel ('Prey'    , fontsize=16)
plt.ylabel ('Predator', fontsize=16)



ax = plt.gca()

sns.move_legend(ax, "upper right")


for item in (\
             #[ax.title,ax.xaxis.label, ax.yaxis.label] + \
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(14)

for item in ax.get_legend().get_texts():
    item.set_fontsize(14)

plt.draw()

#plt.savefig('Figure-5a.eps', format='eps')
#plt.savefig('Figure-5a.png', dpi=400)

plt.show()


